package com.yc.supermarket.service.impl;



import com.yc.supermarket.entity.Admin;
import com.yc.supermarket.mapper.AdminMapper;
import com.yc.supermarket.service.IAdminBiz;
import com.yc.supermarket.util.MybatisUtil;
import com.yc.supermarket.util.ResultEnum;
import com.yc.supermarket.vo.ResultVO;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminBizImpl implements IAdminBiz {
    private AdminMapper mapper;

    @Override
    public int add(Admin admin){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(AdminMapper.class);
        int i= mapper.addmin(admin);
        System.out.println("whatwhy:"+i);
        if(i<=0){
            return 0;
        }
         return 1;
    }

    @Override
    public  Map<String,Object> findPage(Admin admin,Integer pageNum,Integer pageSize){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(AdminMapper.class);
        System.out.println("fuledf:"+pageNum+pageSize);
        List<Admin> list =mapper.find(admin,pageNum,pageSize);
        int total =mapper.total(admin);
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",total);
        map.put("data",list);
        return map;
    }

    @Override
    public ResultVO find(Admin admin) {
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(AdminMapper.class);
        List<Admin> list =mapper.find(admin,null,null);
        if(null==list || list.isEmpty()){
            return new ResultVO(ResultEnum.DATA_NULL);
        }

        return new ResultVO(0,"",list);
    }

    @Override
    public ResultVO updateState(Admin admin){
        SqlSession session= MybatisUtil.getSession();
        mapper=session.getMapper(AdminMapper.class);
        List<Admin> list =mapper.find(admin,null,null);
        if(null==list || list.isEmpty()){
            return new ResultVO(ResultEnum.DATA_NULL);
        }
        Admin obj=list.get(0);
        if(obj.getAdminStatus()==1){
            admin.setAdminStatus(0);
        }else{
            admin.setAdminStatus(1);
        }
        int rs =mapper.updateStatus(admin);
        if(rs<=0){
            return new ResultVO(ResultEnum.ERROR);
        }
        return new ResultVO(ResultEnum.SUCCESS);
    }

    @Override
    public ResultVO login(Admin admin) {
        return null;
    }
}
